//
//  BaseNavigationController.m
//  随时随地测试项目
//
//  Created by 陈锋 on 2017/5/31.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "BaseNavigationController.h"
#import "MMExampleDrawerVisualStateManager.h"
#import "MMDrawerVisualState.h"
#import "MMDrawerController.h"

@interface BaseNavigationController ()<UIGestureRecognizerDelegate>{
    UIImageView *navBarHairlineImageView;
    UIImageView *tabBarHairlineImageView;
}

@property (nonatomic,strong) MMDrawerController * drawerController;
@end

@implementation BaseNavigationController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupNavigateionBar];
    
    self.interactivePopGestureRecognizer.delegate = self;
    
    self.navigationController.navigationBar.translucent = YES;
    
    self.tabBarController.tabBar.hidden = YES;

}

#pragma mark - Private Methods

- (void)setupNavigateionBar
{
    
    NSDictionary* dict = @{ NSForegroundColorAttributeName : [UIColor whiteColor],
                            NSFontAttributeName : [UIFont systemFontOfSize:18.0f] };
    self.navigationBar.titleTextAttributes = dict;
    
        UIImage *image = [UIImage imageNamed:kNavImgName];
         [self.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    
}

/**
 *  重写push方法,添加统一的返回按钮
 */
- (void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated
{
    /*
    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    
    LeftController *leftVC = [[LeftController alloc] init];
    
    self.drawerController = [[MMDrawerController alloc]initWithCenterViewController:viewController rightDrawerViewController:leftVC];
    [self.drawerController setShowsShadow:YES];
    [self.drawerController setMaximumRightDrawerWidth:SCREEN_WIDTH-100];
    [self.drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [self.drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    [self.drawerController setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) {
        
        MMDrawerControllerDrawerVisualStateBlock block;
        block = [[MMExampleDrawerVisualStateManager sharedManager]
                 drawerVisualStateBlockForDrawerSide:drawerSide];
        if(block){
            block(drawerController, drawerSide, percentVisible);
        }
        
    }];//侧滑效果
    
    [[[UIApplication sharedApplication].delegate window] setRootViewController:self.drawerController];
     */
    self.bakcRootView = NO;
    self.tabBarController.tabBar.hidden = YES;

    self.navigationController.navigationBarHidden = NO;
    
    if (self.childViewControllers.count >= 1) {
        self.navigationBar.translucent = NO;
        UIButton* backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setImage:[UIImage imageNamed:@"nav_icon_fh"]
                    forState:UIControlStateNormal];
        [backButton sizeToFit];
        [backButton addTarget:self
                       action:@selector(backAction)
             forControlEvents:UIControlEventTouchUpInside];
        
        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        viewController.navigationItem.leftBarButtonItem =
        [[UIBarButtonItem alloc] initWithCustomView:backButton];
        
        UIButton* menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [menuBtn setImage:[UIImage imageNamed:@"nav_icon_ch"]
                    forState:UIControlStateNormal];
        [menuBtn sizeToFit];
        [menuBtn addTarget:self
                       action:@selector(menuBtn)
             forControlEvents:UIControlEventTouchUpInside];
        
        menuBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        menuBtn.frame = CGRectMake(0, 0, 20, 20);
 /*       viewController.navigationItem.rightBarButtonItem =
        [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
  */
    }
    
    [super pushViewController:viewController animated:animated];
}

-(void)menuBtn{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
}
- (void)backAction
{
    if (self.bakcRootView) {
        
        [self popToRootViewControllerAnimated:YES];
        
    }else{
    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModePanningCenterView];
    [self popViewControllerAnimated:YES];
    }
    
    self.bakcRootView = NO;
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(nonnull UIGestureRecognizer*)gestureRecognizer
{
    return self.childViewControllers.count > 1;
}

@end
